{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c6e6dff",
   "metadata": {},
   "source": [
    "## week07\n",
    "### 列表的基础用法2.0\n",
    "#### 回顾week06："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2d6d0d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_str = \"Alex,Marry,Jack\"\n",
    "names_list = [\"Alex\",\"Marry\",\"Jcak\"]\n",
    "\n",
    "# print(names_str[0:4]) # 字符串，无法确定名字长度 用切片的复杂操作\n",
    "# print(names_list[0]) # 用index索引的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e588ba3",
   "metadata": {},
   "source": [
    "### list的方法\n",
    "#### 思考：对于一个序列数据，我们可以做什么操作？\n",
    "### 1.取值：切片\n",
    "### 2.增加/新增数据，stu_网新1班 =[...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2190b5d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c00ef13d",
   "metadata": {},
   "source": [
    "### 2.1 append() 圆括号的内容是 对象（比较大的一个范围，python中一切皆是对象）\n",
    "###     append() 新增在末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c97f25d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"xiaozeng\")\n",
    "names.append(\"xiaoyao\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c341ff3",
   "metadata": {},
   "source": [
    "### 2.2 insert() 指定位置新增数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "230dc225",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(2,\"xiaole\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a2e83b",
   "metadata": {},
   "source": [
    "### 3.删除\n",
    "### 3.1 remove() 的操作应该先做查询，判断元素value是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "667a32f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.remove(\"xiaozeng\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff162d1f",
   "metadata": {},
   "source": [
    "* 先做查询： seq的一般方法（通用方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18898da1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(\"lihua\" in names) # False/True\n",
    "# username = input(\"names:\")\n",
    "# if username in names:\n",
    "    # 删除该用户\n",
    "#     names.remove(username)\n",
    "# else:\n",
    "#     names.append(username)\n",
    "# print(names)\n",
    "# names.remove(\"lihua\")\n",
    "# print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce271f3e",
   "metadata": {},
   "source": [
    "### 3.2 pop() 在使用pop()之前，要先了解list的长度,清楚删除的索引位置.\n",
    "### 要先了解list的长度 seq 一般方法 len()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76d4aac4",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(names))\n",
    "print(names)\n",
    "names.pop(2)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fade677",
   "metadata": {},
   "source": [
    "#### 清楚删除的索引位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2de6a385",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(names.index(\"Jan\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2868f8ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例： 删除一些具有特征的索引值\n",
    "# for i in range(0,len(names)-1,2):\n",
    "#     names.pop(i)\n",
    "# print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b0eaa38",
   "metadata": {},
   "source": [
    "### 列表的高级用法_枚举2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97eff2e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1 = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names_网新2 = [\"Alex\",\"xiaozeng\",\"xiaoyao\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ebdf633",
   "metadata": {},
   "source": [
    "### 4.删除列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ea16f13",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90b819c6",
   "metadata": {},
   "source": [
    "### 5.合并列表 extend 是有顺序的，（）里面的list排在后面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b72ea94",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新2.extend(names_网新1)\n",
    "print(names_网新2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc70c95d",
   "metadata": {},
   "source": [
    "### 6. copy() 复制列表  赋值 不等于 真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9136a86",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1复制 = names_网新1\n",
    "print(names_网新1)\n",
    "print(names_网新1复制)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1复制:\",names_网新1复制)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "446ecca5",
   "metadata": {},
   "source": [
    "### 列表的高级用法_copy()2.0\n",
    "### 6. copy() 复制列表  赋值 不等于 真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07c7a3d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1复制 = names_网新1\n",
    "print(names_网新1)\n",
    "print(names_网新1复制)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1复制:\",names_网新1复制)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d48d20c",
   "metadata": {},
   "source": [
    "#### 引入copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1157721",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1_源数据备份 = names_网新1.copy()\n",
    "print(names_网新1)\n",
    "print(names_网新1_源数据备份)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1_copy:\",names_网新1_源数据备份)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a81e4b1",
   "metadata": {},
   "source": [
    "#### 用切片 [:] 等价于 copy()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50795274",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1_源数据备份 = names_网新1[:]\n",
    "print(names_网新1)\n",
    "print(names_网新1_源数据备份)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1_源数据备份:\",names_网新1_源数据备份)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a43d47a",
   "metadata": {},
   "source": [
    "### 7. count() 查看元素出现的次数/统计数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02fa4059",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [95,97,90,90,97,88,90]\n",
    "print(stu_id.count(90))# 查看90出现的次数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bef7cdeb",
   "metadata": {},
   "source": [
    "### 8. sort() 排序 reverse=True 从高到低，默认从低到高排"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23cf9c6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [95,97,90,90,97,88,90]\n",
    "stu_id.sort(reverse=True)\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e69636c0",
   "metadata": {},
   "source": [
    "### week07 课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2f2e5a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # 把字符串转变为 list ： list(str)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# pop() 按index索引删除 如果不填则删除末位  对应记忆==>append()增加末位\n",
    "\n",
    "# 循环4次 进行四次plist的pop()方法操作，即删除4次末位\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "print(plist)\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "#                  a     ,      p\n",
    "print(plist)\n",
    "# 删除 位置3的 ‘ ’ 并且插入到  位置 2\n",
    "plist.insert(2,plist.pop(3))\n",
    "print(plist)\n",
    "#\n",
    "new_phrase = ''.join(plist) # 列表元素的合并\n",
    "print(\"plist=\",plist)\n",
    "print(\"new_phrase=\",new_phrase)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
